<?php
require_once 'header.php';
require_once 'common.php';
define('JS_CSS_VERSION', '20130917');
header('Content-Type: text/html;charset=utf-8');

$obj_project = new Project;
$str_action = $_GET['act'] ? trim($_GET['act']) : 'app';
$obj_project->$str_action($str_action);

class Project {
    
    const CACHE_EXPIRE    = 86400;
    const CACHE_PAGE      = 5;
    
    public function app( $str_action ) {
        
        $int_page_size  = 10;
        $str_type       = $_REQUEST['type'] ? trim($_REQUEST['type']) : 'hot';
        $int_page_id    = $_REQUEST['page_id'] ? intval($_REQUEST['page_id']) : 1;
        $int_is_ajax    = intval($_REQUEST['is_ajax']);
        $int_clear_mem  = intval($_REQUEST['clear_mem']);

        // Ajax
        if ( $int_is_ajax ) {

            $bln_mem_cache = $int_page_id <= self::CACHE_PAGE ? true : false;
            
            if ( $bln_mem_cache ) {
                $str_mem_key    = md5(__METHOD__ . $str_type . $int_page_id);
                $obj_mem        = connect_memcache();
                $arr_info       = $obj_mem->get($str_mem_key);
            }
            
            if ( empty($arr_info) || ! $bln_mem_cache || $int_clear_mem ) {
            
                $obj_db         = connect_mysql();
                
                $int_order_by   = $str_type == 'hot' ? 4 : 3;
                $arr_info       = App::get_app_list_info($obj_db, App::APP_SUPPORT_IPHONE, App::APP_TYPE_NOT_GAME, $int_order_by, $int_page_id, $int_page_size, array('file_id'=>App::APP_FILE_ID_MOBILE));
                close_mysql($obj_db);
                
                if ( $bln_mem_cache ) {
                    $obj_mem->set($str_mem_key, $arr_info, MEMCACHE_COMPRESSED, self::CACHE_EXPIRE);
                    $obj_mem->close();
                }
            }

            if ( ! empty($arr_info) ) {
                ob_start();
                include 'html/iphone/app_list_content.html';
                $str_content = ob_get_clean();
            }
            
            output_json_succ($str_content);
        }
        
        $str_title      = '应用';
        
        include 'html/iphone/app_list.html';
    }

    public function game( $str_action ) {
        
        $int_page_size  = 10;
        $str_type       = $_REQUEST['type'] ? trim($_REQUEST['type']) : 'hot';
        $int_page_id    = $_REQUEST['page_id'] ? intval($_REQUEST['page_id']) : 1;
        $int_is_ajax    = intval($_REQUEST['is_ajax']);
        $int_clear_mem  = intval($_REQUEST['clear_mem']);

        // Ajax
        if ( $int_is_ajax ) {

            $bln_mem_cache  = $int_page_id <= self::CACHE_PAGE ? true : false;
            
            if ( $bln_mem_cache ) {
                $str_mem_key    = md5(__METHOD__ . $str_type . $int_page_id);
                $obj_mem        = connect_memcache();
                $arr_info       = $obj_mem->get($str_mem_key);
            }
            
            if ( empty($arr_info) || ! $bln_mem_cache || $int_clear_mem ) {
                
                $obj_db         = connect_mysql();
                
                $int_order_by   = $str_type == 'hot' ? 4 : 3;
                $arr_info       = App::get_app_list_info($obj_db, App::APP_SUPPORT_IPHONE, App::APP_TYPE_GAME, $int_order_by, $int_page_id, $int_page_size, array('file_id'=>App::APP_FILE_ID_MOBILE));
                close_mysql($obj_db);
                
                if ( $bln_mem_cache ) {
                    $obj_mem->set($str_mem_key, $arr_info, MEMCACHE_COMPRESSED, self::CACHE_EXPIRE);
                    $obj_mem->close();
                }
            }
            
            if ( ! empty($arr_info) ) {
                ob_start();
                include 'html/iphone/app_list_content.html';
                $str_content = ob_get_clean();
            }
            
            output_json_succ($str_content);
        }
        
        $str_title      = '游戏';
        
        include 'html/iphone/app_list.html';
    }

    public function category( $str_action ) {
        
        $str_type       = $_REQUEST['type'] ? trim($_REQUEST['type']) : 'game';
        $int_type       = $str_type == 'app' ? App::APP_TYPE_NOT_GAME : App::APP_TYPE_GAME;
        $int_page_id    = $_REQUEST['page_id'] ? intval($_REQUEST['page_id']) : 1;
        $int_is_ajax    = intval($_REQUEST['is_ajax']);
        $int_clear_mem  = intval($_REQUEST['clear_mem']);
        
        // Ajax
        if ( $int_is_ajax ) {
            
            $str_mem_key    = md5(__METHOD__ . $int_type);
            $obj_mem        = connect_memcache();
            $arr_info       = $obj_mem->get($str_mem_key);
            
            if ( empty($arr_info) || $int_clear_mem ) { 
                
                $obj_db         = connect_mysql();
                $arr_info       = array();
                
                if ( $int_type == App::APP_TYPE_NOT_GAME ) {
                    
                    $arr_genre  = App::get_genre_info_by_parent_id($obj_db);
                    unset($arr_genre[App::APP_CATEGORY_ID_GAME]);
                    
                } else {
                    
                    $arr_genre  = App::get_genre_info_by_parent_id($obj_db, App::APP_CATEGORY_ID_GAME);
                }
                
                // Icon, title
                foreach ( $arr_genre as $sub ) {
                    
                    $arr_data               = $sub;
                    $arr_params             = array(
                                                    'file_id'   => App::APP_FILE_ID_MOBILE,
                                                    'genre_id'  => $sub['GENRE_ID'],
                                                    'sub_genre' => $sub['SUB_GENRE'],
                                                    'app_ids'   => $sub['APP_IDS'],
                                              );
                    $arr_list               = App::get_app_list_info($obj_db, App::APP_SUPPORT_IPHONE, $int_type, null, 1, 10, $arr_params);
                    $arr_data['ICON']       = $arr_list[0]['ICON'];
                    $arr_data['GENRE_INFO'] = implode_array_key($arr_list, 'APP_TITLE');
                    
                    $arr_info[]             = $arr_data;
                }
        
                $obj_mem->set($str_mem_key, $arr_info, MEMCACHE_COMPRESSED, self::CACHE_EXPIRE);
                $obj_mem->close();
            }
            
            if ( ! empty($arr_info) ) {
                ob_start();
                include 'html/iphone/app_list_content_category.html';
                $str_content = ob_get_clean();
            }
                
            output_json_succ($str_content);
        }
        
        $str_title      = '分类';
        
        include 'html/iphone/app_list.html';
    }

    public function category_app( $str_action ) {

        $int_page_size  = 10;
        $int_page_id    = $_POST['page_id'] ? intval($_POST['page_id']) : 1;
        $int_is_ajax    = intval($_POST['is_ajax']);
        $int_genre_id   = $_REQUEST['genre_id'] ? intval($_REQUEST['genre_id']) : 6015;
        $int_sub_genre  = intval($_REQUEST['sub_genre']);
        $str_type       = $int_genre_id == App::APP_CATEGORY_ID_GAME ? 'game' : 'app';

        // Ajax
        if ( $int_is_ajax ) {
            
            $obj_db         = connect_mysql();

            $arr_params     = array(
                                    'file_id'   => App::APP_FILE_ID_MOBILE,
                                    'genre_id'  => $int_genre_id,
                                    'sub_genre' => $int_sub_genre
                              );
            $arr_info       = App::get_app_list_info($obj_db, App::APP_SUPPORT_IPHONE, App::APP_TYPE_ALL, 2, $int_page_id, $int_page_size, $arr_params);
            $str_content       = '';
            if ( ! empty($arr_info) ) {
                ob_start();
                include 'html/iphone/app_list_content.html';
                $str_content = ob_get_clean();
            }
            
            output_json_succ($str_content);
        }
        
        $obj_db         = connect_mysql();
        $str_genre_name = App::get_genre_name($obj_db, $int_genre_id);
        $str_title      = $str_genre_name;
        
        include 'html/iphone/app_list_info.html';
    }

    public function search( $str_action ) {

        $arr_search_history = unserialize($_COOKIE['search_history']);
        $int_is_ajax        = intval($_POST['is_ajax']);

        $arr_search = array(
                            '水果忍者', 
                            'QQ', 
                            '微信', 
                            '蜘蛛猎手', 
                            '360', 
                            '斗地主', 
                            '壁纸', 
                            '手电筒', 
                            '拼图罐头', 
                            '百度魔图'
                      );

        if ( $int_is_ajax ) {
            ob_start();
            include 'html/iphone/app_search_content.html';
            $str_content = ob_get_clean();
            output_json_succ($str_content);
        }
        
        include 'html/iphone/app_list.html';
    }

    public function search_result( $str_action ) {

        $int_page_size  = 10;
        $int_page_id    = $_POST['page_id'] ? intval($_POST['page_id']) : 1;
        $int_is_ajax    = intval($_POST['is_ajax']);
        $str_search_word = remove_xss($_REQUEST['search_word']);

        // Ajax
        if ( $int_is_ajax ) {
            
            $str_content = '';
            
            if ( ! empty($str_search_word) ) {

                // Save search history
                $arr_search_history = unserialize($_COOKIE['search_history']);
                $arr_search_history[$str_search_word] = $str_search_word;
                setcookie('search_history', serialize($arr_search_history), time() + 3600 * 24 * 7, '/');
                
                $obj_db          = connect_mysql();
                
                $arr_params      = array(
                                         'file_id'      => App::APP_FILE_ID_MOBILE,
                                         'search_word'  => $str_search_word
                                   );
                $arr_info        = App::get_app_list_info($obj_db, App::APP_SUPPORT_IPHONE, App::APP_TYPE_ALL, 2, $int_page_id, $int_page_size, $arr_params);
                if ( ! empty($arr_info) ) {
                    ob_start();
                    include 'html/iphone/app_list_content.html';
                    $str_content = ob_get_clean();
                } 
            }
            
            output_json_succ($str_content);
        }
        
        $str_title = '搜索';
        
        include 'html/iphone/app_list.html';
    }

    public function app_info( $str_action ) {

        $int_app_id      = intval($_GET['id']);
        $str_back_act    = trim($_GET['back_act']);
        $str_back_type   = trim($_GET['back_type']);
        $str_search_word = trim($_GET['search_word']);
        $int_genre_id    = trim($_GET['genre_id']);
        $int_sub_genre   = trim($_GET['sub_genre']);
        
        $obj_db          = connect_mysql();

        $arr_params      = array(
                                 'file_id'      => App::APP_FILE_ID_MOBILE,
                                 'app_id'       => $int_app_id
                           );

        $arr_info        = App::get_app_list_info($obj_db, App::APP_SUPPORT_IPHONE, App::APP_TYPE_ALL, 2, 1, 1, $arr_params);
        $arr_detail      = $arr_info[0];
        $int_cate_id     = $arr_detail['SUB_GENRE'] ? $arr_detail['SUB_GENRE'] : $arr_detail['GENRE_ID'];
        $str_genre_name  = App::get_genre_name($obj_db, $int_cate_id);
        $arr_imgs        = unserialize($arr_detail['IMGS']);
        $str_back_url    = '/iphone_index.php?act=' . $str_back_act . '&type=' . $str_back_type . '&search_word=' . $str_search_word . '&genre_id=' . $int_genre_id . '&sub_genre=' . $int_sub_genre . '#anchor' . $int_app_id;

        include 'html/iphone/app_info.html';
    }
}
